﻿using Set_File_Rw;
using System;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Windows;
using TestFireBroadcastingHostPhone.ViewModel;

namespace TestFireBroadcastingHostPhone.FrockBorad
{

    public class FrockControl: FrockBorad
    {
        /// <summary>
        /// 打开工装板串口，并设置工装板IO为推挽输出模式
        /// </summary>
        /// <param name="err_info">错误信息</param>
        private void Open_FrockBoradSerialPort(out string err_info)
        {
            err_info = string.Empty;
            string portname = string.Empty;
            string default_file_path = Directory.GetCurrentDirectory() + "\\RecordData\\RelatedRecord.ini"; ;
            if (!string.IsNullOrEmpty(SystemConfigMV.systemconfigMV.Cmb_FrockSerialPort_SelectValue))
            {
                if (SystemConfigMV.systemconfigMV.AreSerialPortsUnique())
                {
                    portname = SystemConfigMV.systemconfigMV.Cmb_FrockSerialPort_SelectValue;
                    try
                    {
                        FrockSerialport = new SerialPort();              // 初始化串口对象
                        FrockSerialport.PortName = portname;             // 设置串口名称
                        FrockSerialport.BaudRate = frockborad_baudrate;  // 设置波特率
                        FrockSerialport.Parity = Parity.None;            // 设置校验位
                        FrockSerialport.DataBits = 8;                    // 设置数据位
                        FrockSerialport.StopBits = StopBits.One;         // 设置停止位              
                        FrockSerialport.Open();// 打开串口
                        Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "工装串口", portname, out err_info);
                        All_PutInit(out err_info);//开启工装板端口初始化
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            err_info += "设置工装板IO口初始化错误：";
                        }
                    }
                    catch (Exception ex)
                    {
                        err_info += "Error opening serial port: " + ex.Message;
                    }
                }
                else
                {
                    err_info += "选择串口存在冲突，请检查串口参数配置!";
                }
            }
            else
            {
                err_info += "请配置工装板串口信息!";
            }
        }

        /// <summary>
        /// 关闭工装串口
        /// </summary>
        public void Close_SerialPort(out string err_info)
        {
            err_info = string.Empty;
            try
            {
                if (FrockSerialport != null)
                {
                    FrockSerialport.Close();
                    FrockSerialport.Dispose();
                    FrockSerialport = null;
                }
            }
            catch (Exception ex)
            {
                err_info += ex.Message;
            }
        }
        /// <summary>
        /// 继电器切换控制
        /// </summary>
        /// <param name="port">端口</param>
        /// <param name="status">true:开/false:闭</param>
        /// <param name="err_info">错误信息</param>
        public void Exchange_ElectricRelay(byte port, bool status, out string err_info)
        {
            err_info = string.Empty;
            if (FrockSerialport == null)
            {
                Open_FrockBoradSerialPort(out err_info);
                if (!string.IsNullOrEmpty(err_info))
                {
                    return;
                }
            }
            Thread.Sleep(100);
            All_PutInit(out err_info);//开启工装板端口初始化
            if (!string.IsNullOrEmpty(err_info))
            {
                err_info += "设置工装板IO口初始化错误：";
            }
            else
            {
                Thread.Sleep(100);
                Set_IOPut(port, status, out err_info);
            }
        }
        /// <summary>
        /// 关闭所有继电器
        /// </summary>
        /// <param name="err_info"></param>
        public void Close_All(out string err_info)
        {
            err_info = string.Empty;
            if (FrockSerialport == null)
            {
                Open_FrockBoradSerialPort(out err_info);
                if (!string.IsNullOrEmpty(err_info))
                {
                    return;
                }
            }
            Thread.Sleep(100);
            Close_AllPutIO_Init(out err_info);//开启工装板端口推挽输出
        }
        /// <summary>
        /// 磁开关检测
        /// </summary>
        /// <param name="err_info">错误信息</param>
        public int Sw_MagneticDetection(out string err_info)
        {
            int ADC_value = 0;
            err_info = string.Empty;
            if (FrockSerialport == null)
            {
                Open_FrockBoradSerialPort(out err_info);
                if (!string.IsNullOrEmpty(err_info))
                {
                    return ADC_value;
                }
            }
            if (FrockSerialport != null)
            {
                ADC_value = Function_ReadADC(out err_info);
            }
            return ADC_value;
        }
        /// <summary>
        /// 开启输出短路测试
        /// </summary>
        /// <param name="err_info"></param>
        public void Start_ShortOutputTest(out string err_info)
        {
            err_info = string.Empty;
            if (FrockSerialport == null)
            {
                Open_FrockBoradSerialPort(out err_info);
                if (!string.IsNullOrEmpty(err_info))
                {
                    return;
                }
            }
            Thread.Sleep(100);
            Set_LEDStatus(0, 1, out err_info);//开启输出短路测试
            if (!string.IsNullOrEmpty(err_info))
            {
                err_info += "开启输出短路失败";
            }
        }
        /// <summary>
        /// 关闭输出短路测试
        /// </summary>
        /// <param name="err_info"></param>
        public void Stop_ShortOutputTest(out string err_info)
        {
            err_info = string.Empty;
            if (FrockSerialport == null)
            {
                Open_FrockBoradSerialPort(out err_info);
                if (!string.IsNullOrEmpty(err_info))
                {
                    return;
                }
            }
            Thread.Sleep(100);
            Set_LEDStatus(0, 0, out err_info);//关闭输出短路测试
            if (!string.IsNullOrEmpty(err_info))
            {
                err_info += "关闭输出短路失败";
            }
        }
        /// <summary>
        /// 切到网口2
        /// </summary>
        /// <param name="err_info"></param>
        public void Net2_RegisterAmplifier(out string err_info)
        {
            err_info = string.Empty;
            if (FrockSerialport == null)
            {
                Open_FrockBoradSerialPort(out err_info);
                if (!string.IsNullOrEmpty(err_info))
                {
                    return;
                }
            }
            Thread.Sleep(100);
            Set_LEDStatus(1, 1, out err_info);//关闭输出短路测试
            if (!string.IsNullOrEmpty(err_info))
            {
                err_info += "切换至网口2失败";
            }
        }
        /// <summary>
        /// 切到网口1
        /// </summary>
        /// <param name="err_info"></param>
        public void Net1_RegisterAmplifier(out string err_info)
        {
            err_info = string.Empty;
            if (FrockSerialport == null)
            {
                Open_FrockBoradSerialPort(out err_info);
                if (!string.IsNullOrEmpty(err_info))
                {
                    return;
                }
            }
            Thread.Sleep(100);
            Set_LEDStatus(1, 0, out err_info);//关闭输出短路测试
            if (!string.IsNullOrEmpty(err_info))
            {
                err_info += "切换至网口1失败";
            }
        }
    }


}
